﻿using System.Threading.Tasks;
using Abp.Authorization;
using Abp.Runtime.Session;
using Partner.Surround.Authorization;
using Partner.Surround.Configuration.Dto;

namespace Partner.Surround.Configuration
{
    /// <summary>
    /// 配置服务应用层实现
    /// </summary>
    [AbpAuthorize]
    public class ConfigurationAppService : SurroundAppServiceBase, IConfigurationAppService
    {
        public async Task ChangeUiTheme(ChangeUiThemeInput input)
        {
            await SettingManager.ChangeSettingForUserAsync(AbpSession.ToUserIdentifier(), AppSettingNames.UiTheme, input.Theme);
        }

        #region 系统设置
        [AbpAuthorize(PermissionNames.Pages_Frame_HostSettings)]
        public async Task<HostSettingsEditDto> GetAllHostSettings()
        {
            return new HostSettingsEditDto
            {
                CompanySettingsEditDto = await GetCompanySettingsAsync()
            };
        }

        [AbpAuthorize(PermissionNames.Pages_Frame_HostSettings)]
        public async Task UpdateAllHostSettings(HostSettingsEditDto input)
        {
            await UpdateCompanySettingsAsync(input.CompanySettingsEditDto);
        }

        #region Company
        /// <summary>
        /// 获取公司相关设置
        /// </summary>
        /// <returns></returns>
        private async Task<CompanySettingsEditDto> GetCompanySettingsAsync()
        {
            return new CompanySettingsEditDto()
            {
                CompanyName = await SettingManager.GetSettingValueForApplicationAsync(AppSettingNames.HostManagement.CompanyName),
                CompanyAddress = await SettingManager.GetSettingValueForApplicationAsync(AppSettingNames.HostManagement.CompanyAddress)
            };
        }

        /// <summary>
        /// 更新公司设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task UpdateCompanySettingsAsync(CompanySettingsEditDto input)
        {
            await SettingManager.ChangeSettingForApplicationAsync(AppSettingNames.HostManagement.CompanyName, input.CompanyName);
            await SettingManager.ChangeSettingForApplicationAsync(AppSettingNames.HostManagement.CompanyAddress, input.CompanyAddress);
        }
        #endregion

        #endregion
    }
}
